package bb.lanxing.activity.team;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.TextureMapView;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;

import bb.lanxing.R;
import bb.lanxing.activity.base.BaseActivity;
import bb.lanxing.manager.SharedManager;
import bb.lanxing.util.BiCiCoorConverter;

public class TeamLocationActivity extends BaseActivity {
    private RelativeLayout mPopWindowView;
    private TextView mGeoAddrView;

    private GeoCoder mGeoCoder = GeoCoder.newInstance();

    private LatLng startLatlng;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_team_location);

        ImageView back = findViewById(R.id.back);
        back.setOnClickListener(v -> onBackPressed());

        TextureMapView textureMapView = findViewById(R.id.textureMapView);
        textureMapView.removeViewAt(1);
        BaiduMap mBaiduMap = textureMapView.getMap();
        mBaiduMap.setOnMapLoadedCallback(() -> {
            LatLng curLatLngWithMP = SharedManager.getInstance().getCurLatLngWithMP();
            startLatlng = new LatLng(getIntent().getDoubleExtra("lat", curLatLngWithMP.latitude),
                    getIntent().getDoubleExtra("lng", curLatLngWithMP.longitude));
            startLatlng = BiCiCoorConverter.earth2Baidu(startLatlng);
            mGeoCoder.reverseGeoCode(new ReverseGeoCodeOption().location(startLatlng));
            mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLngZoom(startLatlng, 17.0f));
            mBaiduMap.addOverlay(new MarkerOptions().position(startLatlng)
                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_marker_route_way_point)));
        });
        mGeoCoder.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() {
            @Override
            public void onGetGeoCodeResult(GeoCodeResult geoCodeResult) {
            }

            @Override
            public void onGetReverseGeoCodeResult(ReverseGeoCodeResult reverseGeoCodeResult) {
                mGeoAddrView.setText(reverseGeoCodeResult.getAddress());
                mBaiduMap.showInfoWindow(new InfoWindow(mPopWindowView, startLatlng, 0));
            }
        });

        mPopWindowView = (RelativeLayout) View.inflate(this, R.layout.activity_team_location_pop_view, null);
        TextView textView = mPopWindowView.findViewById(R.id.poi_address);
        textView.setText(getIntent().getStringExtra("StartAddr"));
        mGeoAddrView = mPopWindowView.findViewById(R.id.geo_address);
    }
}